package ControlLayer;

import ModelLayer.*;

import DBLayer.*;



/**
 * CtrEmp.java
 * 
 * @author kbh
 * @version 5. oktober 2006
 */
public class CustomerCtr {

	private static CustomerCtr instance;
	
	/** Creates a new instance of CtrEmp */
	private CustomerCtr() {

	}
	
	public static void main(String[] args){
		String cprNo="22";
		String fName="Dan";
		String lName="Coconetu";
		String address="prinsensgade";
		String phoneNo="232";
		CustomerCtr ctr= new CustomerCtr();
		ctr.insertNew(cprNo,fName,lName,address,new ZipCode(22),phoneNo,new Club(22));
	}

	public void insertNew(String cprNo, String fname, String lname,
			String address, ZipCode zipcode, String phoneNo, Club club) {
		Customer custObj = new Customer(cprNo, fname, lname, address, zipcode,phoneNo, club);

		DbConnection1.startTransaction();
		DBCustomer dbCustomer = new DBCustomer();
		try {
			dbCustomer.insertCustomer(custObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!DbConnection1.commitTransaction())
			DbConnection1.rollbackTransaction();
	}

	/**
	 * Find and return the customer according to CPR
	 * 
	 * @param cpr
	 * @return customer if found. Else return null
	 */
	public Customer findCustomer(String cpr) {
		DBCustomer dbCustomer= new DBCustomer();
		return dbCustomer.findCustomer(cpr,true);

	}
	
	public void deleteCustomer(String cpr)
	{
	DBCustomer dbCustomer= new DBCustomer();
	dbCustomer.delete(new Customer(cpr));
	}

	/**
	 * The method finds if there is a club with the given clubNo,finds a ZipCode object and creates 
	 * a customer with it and the given information.
	 * @param cprNo
	 * @param fName
	 * @param lName
	 * @param address
	 * @param phoneNo
	 * @param zipCode
	 * @param club
	 * @return 
	 */
	public Customer createCustomer(String cprNo, String fName, String lName,
			String address, String phoneNo, int zipCode,int clubNo) {
		DBZipCode dbZipCode= new DBZipCode();
		DBClub dbClub= new DBClub();
		ZipCode zipCodeObj= dbZipCode.findZipCode(zipCode, true);
		Club club= dbClub.findClub(clubNo,true);
		Customer custObj = new Customer(cprNo, fName, lName, address, zipCodeObj,
				phoneNo, club);

		DbConnection1.startTransaction();
		DBCustomer dbCustomer = new DBCustomer();
		try {
			dbCustomer.insertCustomer(custObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!DbConnection1.commitTransaction())
			DbConnection1.rollbackTransaction();
		
		
		return custObj;

	}
	
	
	public void updateCustomer(String cprNo, String fName, String lName, String address, int pCode, String phoneNo, int clubId)
	{
		Customer customer= new Customer(cprNo,fName,lName,address,new ZipCode(pCode),phoneNo, new Club(clubId));
		DBCustomer dbCustomer= new DBCustomer();
		dbCustomer.updateCustomer(customer);
	}

	
	public static CustomerCtr getInstance()
	 {
	  if(instance == null){
	   instance = new CustomerCtr();
	  }
	  
	  return instance;
	 }
}
